/**
 * 
 */
package com.vgtech.vantop;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * app下载
 */
@Controller
@RequestMapping(value = "/apk")
public class DownLoadAppCotroller {

	@RequestMapping(value = "/createQRCode.do")
	public void createQRCode(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("create qrcode start...");
		String localURL = PropertiesFactory.getValue("qrcodePath");
		String imgName = PropertiesFactory.getValue("qrcodeFile");
		String path = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + request.getContextPath();
		String text = path + "/apk/down.do";
		System.out.println(text);
		// String text = "http://192.168.2.27:8080/vantop-sba-web/apk/down.do";
		QRCodeUtil.createQRCode(localURL, imgName, text);
		System.out.println("qrcode path: "+localURL+"\\"+imgName);
		System.out.println("create qrcode end...");
	}

	@RequestMapping(value = "/download.do")
	public void download(HttpServletRequest request,
			HttpServletResponse response) {
		String ostype = request.getParameter("ostype");

		// 默认是android
		String downFileName = "";
		String downFilePath = "";
		if ("android".equals(ostype)) {
			downFileName = PropertiesFactory.getValue("fileName-android");
			downFilePath = PropertiesFactory.getValue("filePath-android");
			response.setHeader("Content-type","apk"); 
		} else {
			downFileName = PropertiesFactory.getValue("fileName-ios");
			downFilePath = PropertiesFactory.getValue("filePath-ios");
		}

		String path = downFilePath + "\\" + downFileName;// 获取文件的相对路径
		String filename = downFilePath + "\\" + downFileName;
		// response.setHeader告诉浏览器以什么方式打开
		// 假如文件名称是中文则要使用 URLEncoder.encode()编码
		// 否则直接使用response.setHeader("content-disposition",
		// "attachment;filename=" + filename);即可
		response.setHeader("Location", downFileName);
		response.setHeader("content-disposition", "attachment;filename="
				+ downFileName);

		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(path); // 获取文件的流
			int len = 0;
			byte buf[] = new byte[4096*2];// 缓存作用
			out = response.getOutputStream();// 输出流
			while ((len = in.read(buf)) > 0) // 切忌这后面不能加 分号 ”;“
			{
				out.write(buf, 0, len);// 向客户端输出，实际是把数据存放在response中，然后web服务器再去response中读取
			}
		} catch (IOException e) {
//			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@RequestMapping(value = "/openDown.do")
	public String openDown(HttpServletRequest request,
			HttpServletResponse response, String ostype) {
		return "/WEB-INF/jsp/app/download";
	}

	/**
	 * 文件下载
	 */
	@RequestMapping(value = "/down")
	public String DownFile(HttpServletRequest request,
			HttpServletResponse response) {
		return "/WEB-INF/jsp/app/down";
	}
}
